package xhwl.hmo.mall.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import xhwl.hmo.mall.Constant;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.FindAdapter;
import xhwl.hmo.mall.event.ThumpupEvent;
import xhwl.hmo.mall.http.api.FindApi;
import xhwl.hmo.mall.http.api.PageViewApi;
import xhwl.hmo.mall.http.api.ThumbsUpApi;
import xhwl.hmo.mall.http.bean.resp.FindResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.ui.FindListActivity;
import xhwl.hmo.mall.utils.AppUtil;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.hmo.mall.views.MyListView;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by admin on 2017-10-20.
 */

public class FindFragment extends BaseFragment implements View.OnClickListener {
    MyListView lv_find;
    FindAdapter findAdapter;
    ImageView img_clinical_interpretation, img_health_exposition, img_product_triall;
    List<FindResp> mlist = new ArrayList();
    int page_num = 1;
    RefreshLayout refreshLayout;
//    Drawable drawable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, null);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView(View view) {
        refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout_find);
        lv_find = (MyListView) view.findViewById(R.id.lv_find);
        img_clinical_interpretation = (ImageView) view.findViewById(R.id.img_clinical_interpretation);
        img_health_exposition = (ImageView) view.findViewById(R.id.img_health_exposition);
        img_product_triall = (ImageView) view.findViewById(R.id.img_product_triall);
        findAdapter = new FindAdapter(mActivity, mlist);
        lv_find.setAdapter(findAdapter);
        findAdapter.setOnAddClickListener(onItemActionClick);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                find(page_num + 1);
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                find(1);
            }
        });

    }


    @Override
    protected void initListener() {
        img_clinical_interpretation.setOnClickListener(this);
        img_health_exposition.setOnClickListener(this);
        img_product_triall.setOnClickListener(this);
        lv_find.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                PageViewApi pageViewapi = new PageViewApi(mlist.get(position).RECORD_ID);
                HttpManager.getInstance().dealHttp(FindFragment.this, pageViewapi, new OnNextListener<HttpResp>() {
                    @Override
                    public void onNext(HttpResp httpResp) {
                        if (httpResp.isOk()) {
                            String url= Constant.ARTICAL_URL+mlist.get(position).RECORD_ID;
                            AppUtil.startWebViewActivity(getActivity(),url,"");
                        }
                    }
                });
            }

        });

    }

    @Override
    protected void loadData() {
        find(1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_clinical_interpretation:
                Intent intent1 = new Intent(mActivity, FindListActivity.class);
                intent1.putExtra("name", 0);
                startActivity(intent1);
                break;
            case R.id.img_health_exposition:
                Intent intent2 = new Intent(mActivity, FindListActivity.class);
                intent2.putExtra("name", 1);
                startActivity(intent2);
                break;
            case R.id.img_product_triall:
                Intent intent3 = new Intent(mActivity, FindListActivity.class);
                intent3.putExtra("name", 2);
                startActivity(intent3);
                break;

        }
    }

    FindAdapter.OnAddClickListener onItemActionClick = new FindAdapter.OnAddClickListener() {

        @Override
        public void onItemClick(final int position) {
            ThumbsUpApi thumbsUpApi = new ThumbsUpApi(SpUtil.getString(SpUtil.MEMBER_MOBILE, ""), mlist.get(position).RECORD_ID);
            HttpManager.getInstance().dealHttp((RxFragmentActivity) mActivity, thumbsUpApi, new OnNextListener<HttpResp>() {
                @Override
                public void onNext(HttpResp httpResp) {
                    if (httpResp.isOk()) {
                        FindResp bean = mlist.get(position);
                        int thumbs = Integer.valueOf(mlist.get(position).THUMBS_UP).intValue();
                        if("1".equals(bean.THUMBS_FLAG)){
                            //当前已点赞
                            bean.THUMBS_FLAG="0";
                            thumbs--;
                        }
                        else
                        {
                            bean.THUMBS_FLAG="1";
                            thumbs++;
                        }
                        bean.THUMBS_UP=thumbs+"";
                        findAdapter.notifyDataSetChanged();
                    } else {
                        ToastUtil.showToast(context, httpResp.message);
                    }

                }
            });
        }
    };

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    public void find(final int page){
        FindApi findApi = new FindApi("", SpUtil.getString(SpUtil.MEMBER_MOBILE, ""),page);
        HttpManager.getInstance().dealHttp((RxFragmentActivity) mActivity, findApi, new OnNextListener<HttpResp<ArrayList<FindResp>>>() {

            @Override
            public void onNext(HttpResp<ArrayList<FindResp>> arrayListHttpResp) {
                if (page > 1) {
                    refreshLayout.finishLoadmore();
                }else
                {
                    refreshLayout.finishRefresh();
                }



                if (arrayListHttpResp.data.size() > 0) {
                    page_num = page;
                    if(page==1)
                    {
                        mlist.clear();
                    }
                    mlist.addAll(arrayListHttpResp.data);
                    findAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(mActivity, getString(R.string.no_more_data));
//                        refreshLayout.setEnableLoadmore(false);
                }


            }
        });

    }
    public void onEvent(ThumpupEvent event){
        loadData();
    }
}
